#include <curl.h>
#include <stdio.h>
#include "cJSON.h"
//回调函数
size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) 
{
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;
    printf("%s\n",result);

    cJSON *jsonstr = cJSON_Parse(result);
    if (jsonstr!=NULL)
    {
        cJSON *successJson = cJSON_GetObjectItem(jsonstr,"success");
        cJSON *codeJson = cJSON_GetObjectItem(jsonstr,"code");
        cJSON *messageJson = cJSON_GetObjectItem(jsonstr,"message");
        //printf("1111111----successJson:%s\n",cJSON_Print(successJson));//解决错误打的断点
         if (cJSON_IsTrue(successJson))
        {
            printf("成功！%s\n",messageJson->valuestring);
         }else{
            printf("失败！%s\n",messageJson->valuestring);
        }
    }
    return realsize;
}

int main(int argc, char const *argv[])
{
    //1.初始化一个curl
    CURL *curl = curl_easy_init();
    //2.给curl设置一些对应的选项(目标地址，回调函数，请求参数，发送文件)
    curl_easy_setopt(curl,CURLOPT_URL,"http://47.109.65.208/config.json");
    //3.发送请求参数
    //curl_easy_setopt(curl,CURLOPT_POSTFIELDS,"xx=sss&yy=000");
    //4.设置回调函数
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writeCallback);
    //5.发送请求
    CURLcode code = curl_easy_perform(curl);
    if (code!=CURLE_OK)
    {
        fprintf(stderr,"curl_easy_perform() failed: %s\n",curl_easy_strerror(code));
    }
    //6.清理
    curl_easy_cleanup(curl);
    return 0;
}
